package tw.asap.plus.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import java.util.ArrayList;

import tw.asap.plus.ui.adapter.PinedSectionAdapter;

/**
 * Created by Yax on 15/11/13.
 */
public class CartDao {


    private DBHelper dbHelper;

    public CartDao(Context context) {
        this.dbHelper = new DBHelper(context);
    }

    public void addGoods(PinedSectionAdapter.GoodsItem goodsItem, String shopKey) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues valuesInfo = new ContentValues();
        valuesInfo.put(DBHelper.GoodsInfoColumns.GOODS_ID, goodsItem.itemEntity.getId());
        valuesInfo.put(DBHelper.GoodsInfoColumns.SHOP_KEY, shopKey);
        valuesInfo.put(DBHelper.GoodsInfoColumns.GOODS_COUNTS, 1);

        Cursor cursor;
        cursor = db.rawQuery("select " + DBHelper.GoodsInfoColumns.GOODS_COUNTS + " from "
                + DBHelper.TABLE_GOODS + " where " + DBHelper.GoodsInfoColumns.GOODS_ID + "='" + goodsItem.itemEntity.getId() + "'", null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            int originCount = cursor.getInt(0);
            cursor.close();

            if (originCount > 0) {
                int newCount = originCount + 1;
                ContentValues values = new ContentValues();
                values.put(DBHelper.GoodsInfoColumns.GOODS_COUNTS, newCount);
                db.update(DBHelper.TABLE_GOODS, values, DBHelper.GoodsInfoColumns.GOODS_ID + "=?", new String[]{goodsItem.itemEntity.getId()});
            } else {
                db.insert(DBHelper.TABLE_GOODS, null, valuesInfo);
            }
        } else {
            db.insert(DBHelper.TABLE_GOODS, null, valuesInfo);
        }
        db.close();
    }


    public void delGoods(PinedSectionAdapter.GoodsItem goodsItem) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        Cursor cursor;
        cursor = db.rawQuery("select " + DBHelper.GoodsInfoColumns.GOODS_COUNTS + " from "
                + DBHelper.TABLE_GOODS + " where " + DBHelper.GoodsInfoColumns.GOODS_ID + "='" + goodsItem.itemEntity.getId() + "'", null);
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            int originCount = cursor.getInt(0);
            cursor.close();

            if (originCount > 0) {
                int newCount = originCount - 1;
                ContentValues values = new ContentValues();
                values.put(DBHelper.GoodsInfoColumns.GOODS_COUNTS, newCount);
                db.update(DBHelper.TABLE_GOODS, values, DBHelper.GoodsInfoColumns.GOODS_ID + "=?", new String[]{goodsItem.itemEntity.getId()});
            } else {
                db.delete(DBHelper.TABLE_GOODS, DBHelper.GoodsInfoColumns.GOODS_ID + "=?", new String[]{goodsItem.itemEntity.getId()});
            }
        } else {
            db.delete(DBHelper.TABLE_GOODS, DBHelper.GoodsInfoColumns.GOODS_ID + "=?", new String[]{goodsItem.itemEntity.getId()});
        }
        db.close();
    }

    public void saveShopCount(int sum, String shopKey) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues valuesInfo = new ContentValues();
        valuesInfo.put(DBHelper.GoodsInfoColumns.SHOP_KEY, shopKey);
        valuesInfo.put(DBHelper.GoodsInfoColumns.SHOP_GOODS_SUM, sum);

        Cursor cursor;
        cursor = db.rawQuery("select * from "
                + DBHelper.TABLE_SHOP + " where " + DBHelper.GoodsInfoColumns.SHOP_KEY + "='" + shopKey + "'", null);
        int count = cursor.getCount();
        cursor.close();

        if (count > 0) {
            db.execSQL("update " + DBHelper.TABLE_SHOP + " set " +
                    DBHelper.GoodsInfoColumns.SHOP_GOODS_SUM + "=" + sum + " where "
                    + DBHelper.GoodsInfoColumns.SHOP_KEY + "=" + shopKey);
        } else {
            db.insert(DBHelper.TABLE_SHOP, null, valuesInfo);
        }
    }


//    public ArrayList<Goods> getUserGoodsInfo(String uid, int start, int count) {
//
//        if (TextUtils.isEmpty(uid)) {
//            return getNoUserGoodsInfo(start, count);
//        } else {
//            SQLiteDatabase db = helper.getReadableDatabase();
//            Cursor cursor = db.query(DBHelper.TABLE_GOODS, null, "uid=?", new String[]{uid}, null, null, null, start + "," + (start + count));
//            ArrayList<Goods> goodsList = new ArrayList<>();
//            while (cursor.moveToNext()) {
//                Goods goods = new Goods();
//                goods.setTitle(cursor.getString(cursor.getColumnIndex("title")));
//                goods.setColor(cursor.getString(cursor.getColumnIndex("color")));
//                goods.setCount(cursor.getString(cursor.getColumnIndex("count")));
//                goods.setId(cursor.getString(cursor.getColumnIndex("id")));
//                goods.setImg(cursor.getString(cursor.getColumnIndex("img")));
//                goods.setSid(cursor.getString(cursor.getColumnIndex("sid")));
//                goods.setSize(cursor.getString(cursor.getColumnIndex("size")));
//                goods.setTm(cursor.getString(cursor.getColumnIndex("tm")));
//                goods.setUid(cursor.getString(cursor.getColumnIndex("uid")));
//                goods.setPrice(cursor.getString(cursor.getColumnIndex("price")));
//                goodsList.add(goods);
//            }
//            cursor.close();
//            db.close();
//            return goodsList;
//        }
//    }
}
